﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.Animation;
using Trippy.Levels;
using Trippy.CollidableGameComponents.Agents;

namespace Trippy
{
    public class DrawableTrippyComponent : DrawableGameComponent
    {
        public AnimatedSprite Sprite { get; protected set; }

        protected bool initialized = false;
        public Vector2 InitialPosition { get; set; }
        protected Vector2 pos;
        public Vector2 Position 
        {
            get
            {
                return pos;
            }
            set
            {
                pos = value;
                if (initialized == false)
                {
                    InitialPosition = pos;
                    initialized = true;
                }
            }
        }
        public Vector2 Velocity { get; set; }
        public Vector2 Acceleration { get; set; } // gravity
        public Vector2 Scale { get; set; }

        protected bool markedForDeletion;

        public Level CurrentLevel { get; protected set; }

        public DrawableTrippyComponent(Game game)
            : base(game)
        {
            Scale = new Vector2(1.0f, 1.0f);
            markedForDeletion = false;
        }

        public virtual void StandardDraw(GameTime gameTime)
        {
            
            Vector2 offset = new Vector2(Sprite.Width / 2.0f, Sprite.Height / 2.0f);
            Sprite.Draw(Position, offset, Scale);
        }

        public virtual void MarkForAddition()
        {
            CurrentLevel.Game.AddQueue.Add(this);
            markedForDeletion = false;
        }

        float deletionTimer;

        public virtual void MarkForDeletion(Hero.MannerOfDeath d, float dt = 0f)
        {
            deletionTimer = dt;
            markedForDeletion = true;
        }

        public virtual void MarkForDeletion(float dt = 0f)
        {
            deletionTimer = dt;
            markedForDeletion = true;
        }

        public virtual void MarkForDeletion()
        {
            markedForDeletion = true;
        }

        public virtual void PreUpdate() //run in loop before update step
        {
            
        }

        public virtual void PostUpdate(float elapsedTime) //run in loop after update step
        {
            if (markedForDeletion)
            {
                deletionTimer -= elapsedTime;
                if(deletionTimer <= 0f)
                    CurrentLevel.Remove(this);
            }
        }
    }
}
